package com.yonyou.pmclouds.plan.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.plan.entity.ProjectPlanViewVO;
import com.yonyou.pmclouds.plan.entity.ScheduleItemVO;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleItemQueryService;
import com.yonyou.pmclouds.plan.rmiitf.ProjectPlanQuery;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Slf4j
@Controller
@RequestMapping(value = "/plan")
public class PlanBoardController {


    @Autowired
    private UserInfoQuery userInfoQuery;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IScheduleItemQueryService scheduleItemQueryService;

    private static final String[] USER_ID_FIELDS = {ProjectVO.MANAGER,ProjectVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {ProjectVO.MANAGER_NAME,ProjectVO.CREATOR_NAME};

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectSimpleQuery query;

    @GetMapping("/queryBoardByPkProSrc")
    @ResponseBody
    public Map<String,Object> queryBoardByPkProSrc(@RequestParam("pkSrc") String pkProSrc) throws BusinessException {

        Map<String, Object> result = query.queryPlanBoardByProSrcId(pkProSrc);
        if(result == null || result .size() == 0 ){
            return new HashMap<>();
        }

        ProjectVO projectVO = (ProjectVO)result.get("projectVO");
        if (projectVO == null) throw new BusinessException("未找到对应的项目");

        userInfoQuery.tranUserInfoField(new Object[]{projectVO}, USER_ID_FIELDS, USER_NAME_FIELDS);

        return result;
    }

    /**
     *
     * @param projectId
     * @return  根据项目id 返回项目看板数据
     */
    @GetMapping("/projectById")
    @ResponseBody
    public Map<String,Object> queryProjectById(@RequestParam("projectId") String projectId,@RequestParam(required = false,name="pkPlan") String pkPlan) throws BusinessException {

        Map<String, Object> result = query.queryProjectBoardById(projectId,pkPlan);
        if(result == null) return new HashMap<String,Object>();

        ProjectVO projectVO = (ProjectVO)result.get("projectVO");

        userInfoQuery.tranUserInfoField(new Object[]{projectVO}, USER_ID_FIELDS, USER_NAME_FIELDS);

        return result;
    }

    @RequestMapping("/getMillstones")
    @ResponseBody
    public ScheduleItemVO[] getMillstones(@RequestParam(value = "projectId",required = false)String projectId,@RequestParam("pkPlan") String pkPlan) throws BusinessException{

        ScheduleItemVO[] scheduleItemVOS = scheduleItemQueryService.queryMileStonePlan(pkPlan);

        return scheduleItemVOS;
    }
}
